package com.zhangyanqiang.traceId.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author zhangyanqiang
 * @date 2020/7/30
 **/
public class WebTraceIdFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(WebTraceIdFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            TraceIdUtil.addTraceId();
            filterChain.doFilter(servletRequest,servletResponse);
        }catch (Exception e){
            logger.error("failed to add web traceId:{}",e);

        }finally {
            TraceIdUtil.removeLogTraceId();
        }
    }

    @Override
    public void destroy() {

    }
}
